//
//  OTDR_ConfigInfoViewController.m
//  OTDR
//
//  Created by Liu on 2024/5/19.
//

#import "OTDR_ConfigInfoViewController.h"
#import "OTDR_DetailCell.h"
#import "OTDR_ConfigInfoModel.h"
#import "OTDR_ChooseAlertView.h"

@interface OTDR_ConfigInfoViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic ,strong)UITableView *tableView;
@property(nonatomic ,strong)NSArray *configArr;

@property(nonatomic ,copy)NSString *modelConfig;
@property(nonatomic ,copy)NSString *waveConfig;
@property(nonatomic ,copy)NSString *avgTimeConfig;
@property(nonatomic ,copy)NSString *iorConfig;
@property(nonatomic ,copy)NSString *resolutionRatioConfig;

@property(nonatomic ,copy)NSString *rangeConfig;
@property(nonatomic ,copy)NSString *pulseConfig;
@property(nonatomic ,copy)NSString *attConfig;

@end

@implementation OTDR_ConfigInfoViewController
-(NSArray *)configArr{
    if (!_configArr) {
        _configArr = [NSArray new];
        _configArr = [OTDR_ConfigInfoModel getConfigInfo];
        
        for (int i=0; i<_configArr.count; i++) {
            NSDictionary *dic = _configArr[i];
            NSArray *arr = dic[@"configInfo"];
            if (i==0) {
                self.modelConfig = arr[0];
            }
            else if (i==1) {
                self.waveConfig = arr[2];
            }
            else if (i==2) {
                self.avgTimeConfig = arr[4];
            }
            else if (i==3) {
                self.iorConfig = arr[16];
            }
            else if (i==4) {
                self.resolutionRatioConfig = arr[0];
            }
            else if (i==5) {
                self.rangeConfig = arr[0];
            }
            else if (i==6) {
                self.pulseConfig = arr[0];
            }
            else{
                self.attConfig = arr[0];
            }
        }
    }
    return _configArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavTitleStr:@""];
    self.view.backgroundColor = kWhiteColor;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(0);
    }];
}

#pragma mark -- tableView
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate   = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 50;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        if (@available(iOS 11.0,*)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
        }
        [_tableView registerClass:[OTDR_DetailCell class] forCellReuseIdentifier:@"OTDR_DetailCell"];
    }
    return _tableView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return SIZE(50);
};
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0001;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([self.modelConfig isEqualToString:@"Auto"]) {
        return self.configArr.count-3;
    }
    return self.configArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    OTDR_DetailCell  *cell = [tableView dequeueReusableCellWithIdentifier:@"OTDR_DetailCell" forIndexPath:indexPath];
    NSDictionary *dic = self.configArr[indexPath.row];
    cell.nameLabel.text = dic[@"name"];
    if (indexPath.row==0) {
        cell.detailInfoLabel.text = self.modelConfig;
    }
    else if(indexPath.row==1){
        cell.detailInfoLabel.text = [NSString stringWithFormat:@"%@nm",self.waveConfig];
    }
    else if(indexPath.row==2){
        cell.detailInfoLabel.text = self.avgTimeConfig;
    }
    else if(indexPath.row==3){
        cell.detailInfoLabel.text = self.iorConfig;
    }
    else if(indexPath.row==4){
        cell.detailInfoLabel.text = self.resolutionRatioConfig;
    }
    else if(indexPath.row==5){
        cell.detailInfoLabel.text = [NSString stringWithFormat:@"%@m",self.rangeConfig];
    }
    else if(indexPath.row==6){
        cell.detailInfoLabel.text = [NSString stringWithFormat:@"%@ns",self.pulseConfig];
    }
    else {
        cell.detailInfoLabel.text = [NSString stringWithFormat:@"%@db",self.attConfig];
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    ws(weakSelf);
    NSDictionary *dic = self.configArr[indexPath.row];
    NSArray *arr = dic[@"configInfo"];
    OTDR_ChooseAlertView *alertView = [OTDR_ChooseAlertView new];
    alertView.pickerBlock = ^(NSString * _Nonnull resultStr) {
        if (indexPath.row==0) {
            weakSelf.modelConfig = resultStr;
        }
        else if(indexPath.row==1){
            weakSelf.waveConfig = resultStr;
        }
        else if(indexPath.row==2){
            weakSelf.avgTimeConfig = resultStr;
        }
        else if(indexPath.row==3){
            weakSelf.iorConfig = resultStr;
        }
        else if(indexPath.row==4){
            weakSelf.resolutionRatioConfig = resultStr;
        }
        else if(indexPath.row==5){
            weakSelf.rangeConfig = resultStr;
        }
        else if(indexPath.row==6){
            weakSelf.pulseConfig = resultStr;
        }
        else{
            weakSelf.attConfig = resultStr;
        }
        [weakSelf.tableView reloadData];
    };
    NSString *showStr = @"";
    if (indexPath.row==0) {
        showStr = self.modelConfig;
    }
    else if(indexPath.row==1){
        showStr = self.waveConfig;
    }
    else if(indexPath.row==2){
        showStr = self.avgTimeConfig;
    }
    else if(indexPath.row==3){
        showStr = self.iorConfig;
    }
    else if(indexPath.row==4){
        showStr = self.resolutionRatioConfig;
    }
    else if(indexPath.row==5){
        showStr = self.rangeConfig;
    }
    else if(indexPath.row==6){
        showStr = self.pulseConfig;
    }
    else{
        showStr = self.attConfig;
    }
    [alertView showInView:[UIApplication sharedApplication].keyWindow sourceDataArray:arr nowShowString:showStr];
}
-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    if (self.chooseConfigInfoBlock) {
        self.chooseConfigInfoBlock(self.modelConfig, self.waveConfig, self.avgTimeConfig, self.iorConfig, self.resolutionRatioConfig,self.rangeConfig,self.pulseConfig,self.attConfig);
    }
}
@end
